package com.surfilter.msmp.common.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author 贾博
 * @title: DocUtil
 * @description: 文本查重工具类
 * @date 2019/6/14
 */
public class DocUtil {

    public static double getSimilarity(String doc1, String doc2) {
        //判断两个文档都不为空
        if (doc1 != null && doc1.trim().length() > 0 && doc2 != null&& doc2.trim().length() > 0) {
            Map<String, int[]> AlgorithmMap = new HashMap<String, int[]>();
            //将文档中的中文加入到

            String ed1 = doc1.replaceAll("[^a-z^A-Z]", " ");
            for (int i = 0; i < doc1.length(); i++) {
                if(isHanZi(doc1.charAt(i))){
                    String d1 =String.valueOf(doc1.charAt(i));
                    int[] fq = AlgorithmMap.get(d1);
                    if(fq != null && fq.length == 2){
                        fq[0]++;
                    }else {
                        fq = new int[2];
                        fq[0] = 1;
                        fq[1] = 0;
                        AlgorithmMap.put(d1, fq);
                    }
                }
            }
            String[] ed1arr = ed1.split(" ");
            for(String temp:ed1arr ){
            if(temp.length()>=1){
                int[] fq = AlgorithmMap.get(temp);
                if(fq!=null){
                    fq[0]++;
                }else{
                    fq = new int[2];
                    fq[0] = 1;
                    fq[1] = 0;
                    AlgorithmMap.put(temp, fq);
                }
            }
            }

            String ed2 = doc2.replaceAll("[^a-z^A-Z]", " ");
            for (int i = 0; i < doc2.length(); i++) {
                if(isHanZi(doc2.charAt(i))){
                    String d2 = String.valueOf(doc2.charAt(i));
                    int[] fq = AlgorithmMap.get(d2);
                    if(fq != null && fq.length == 2){
                        fq[1]++;
                    }else {
                        fq = new int[2];
                        fq[0] = 0;
                        fq[1] = 1;
                        AlgorithmMap.put(d2, fq);
                    }
                }
            }

            String[] ed2arr = ed2.split(" ");
            for(String temp:ed2arr ){
             if(temp.length()>=1){
                 int[] fq = AlgorithmMap.get(temp);
                 if(fq != null){
                     fq[1]++;
                 }else {
                     fq = new int[2];
                     fq[0] = 0;
                     fq[1] = 1;
                     AlgorithmMap.put(temp, fq);
                 }
             }

            }


            Iterator<String> iterator = AlgorithmMap.keySet().iterator();
            double sqdoc1 = 0;
            double sqdoc2 = 0;
            double denominator = 0;
            while(iterator.hasNext()){
                int[] c = AlgorithmMap.get(iterator.next());
                denominator += c[0]*c[1];
                sqdoc1 += c[0]*c[0];
                sqdoc2 += c[1]*c[1];
            }
            return denominator / Math.sqrt(sqdoc1*sqdoc2);//余弦计算
        }else {
            throw new NullPointerException(" 文本不能为空");
        }



        }


      /**
       * @description: 判断是否为汉字
       * @throws
       * @author 贾博
       * @date 2019/6/14
       */
    public static boolean isHanZi(char ch) {
        // 判断是否汉字  
        return (ch >= 0x4E00 && ch <= 0x9FA5);  
    }



    public static void main(String[] args) {
        String s1=" vpn my name is teat ";
        String s2="vpnsa masasy naasasasasasasaasasasasasasasasasame is teat ";
        double similarity = getSimilarity(s1, s2);
        System.out.println("两段文本的相似度为："+similarity);

    }


}
